<template>
  <div class="page-container">
    <!-- 顶部返回按钮 -->
    <div class="header">
      <div class="back-btn" @click="goBack">
        <Left />
      </div>
      <h1 class="title">登录</h1>
      <div class="placeholder"></div>
    </div>

    <!-- 登录表单 -->
    <div class="login-form">
      <div class="form-header">
        <h2>欢迎回来</h2>
        <p>登录您的账户以继续购物</p>
      </div>

      <div class="form-content">
        <div class="form-item">
          <label class="form-label">用户名/手机号</label>
          <nut-input 
            v-model="formData.username" 
            placeholder="请输入用户名或手机号"
            type="text"
            class="form-input"
          />
        </div>

        <div class="form-item">
          <label class="form-label">密码</label>
          <nut-input 
            v-model="formData.password" 
            placeholder="请输入密码"
            type="password"
            class="form-input"
          />
        </div>

        <div class="form-options">
          <nut-checkbox v-model="rememberMe" label="记住我" />
          <span class="forgot-password" @click="goToForgotPassword">忘记密码？</span>
        </div>

        <div class="form-actions">
          <nut-button 
            type="primary" 
            size="large"
            :loading="loading"
            :disabled="!formData.username || !formData.password"
            @click="handleLogin"
            class="login-btn"
          >
            {{ loading ? '登录中...' : '登录' }}
          </nut-button>
        </div>

        <div class="divider">
          <span class="divider-text">或</span>
        </div>

        <div class="social-login">
          <div class="social-btn wechat" @click="socialLogin('wechat')">
            <img src="https://picsum.photos/24/24?random=1" alt="微信" />
            <span>微信登录</span>
          </div>
          <div class="social-btn qq" @click="socialLogin('qq')">
            <img src="https://picsum.photos/24/24?random=1" alt="QQ" />
            <span>QQ登录</span>
          </div>
        </div>

        <div class="register-link">
          <span>还没有账户？</span>
          <span class="link" @click="goToRegister">立即注册</span>
        </div>
      </div>
    </div>

    <!-- 底部协议 -->
    <div class="agreement">
      <p>登录即表示您同意我们的</p>
      <p>
        <span class="link" @click="goToTerms">服务条款</span>
        和
        <span class="link" @click="goToPrivacy">隐私政策</span>
      </p>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { Left } from '@nutui/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 表单数据
const formData = reactive({
  username: '',
  password: ''
})

// 记住我
const rememberMe = ref(false)

// 加载状态
const loading = ref(false)

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 处理登录
const handleLogin = async () => {
  if (!formData.username || !formData.password) {
    showToast('请填写完整的登录信息')
    return
  }

  loading.value = true

  try {
    const result = await userStore.login(formData)
    if (result.success) {
      showToast('登录成功')
      // 跳转到之前的页面或首页
      setTimeout(() => {
        router.push('/')
      }, 1000)
    }
  } catch (error) {
    showToast(error.message || '登录失败，请重试')
  } finally {
    loading.value = false
  }
}

// 社交登录
const socialLogin = (platform) => {
  showToast(`${platform === 'wechat' ? '微信' : 'QQ'}登录功能开发中`)
}

// 跳转到注册页
const goToRegister = () => {
  router.push('/register')
}

// 跳转到忘记密码页
const goToForgotPassword = () => {
  showToast('忘记密码功能开发中')
}

// 跳转到服务条款
const goToTerms = () => {
  showToast('服务条款页面开发中')
}

// 跳转到隐私政策
const goToPrivacy = () => {
  showToast('隐私政策页面开发中')
}

// 显示提示
const showToast = (message) => {
  // 这里可以使用NutUI的Toast组件
  console.log(message)
}
</script>

<style scoped>
.header {
  background: white;
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #666;
}

.title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.placeholder {
  width: 24px;
}

.login-form {
  flex: 1;
  padding: 32px 24px;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

.form-header p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.form-content {
  max-width: 400px;
  margin: 0 auto;
}

.form-item {
  margin-bottom: 24px;
}

.form-label {
  display: block;
  font-size: 14px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 500;
}

.form-input {
  width: 100%;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
}

.forgot-password {
  font-size: 14px;
  color: #666;
  cursor: pointer;
}

.forgot-password:hover {
  color: #ff4757;
}

.form-actions {
  margin-bottom: 24px;
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
}

.divider {
  position: relative;
  text-align: center;
  margin: 32px 0;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #f0f0f0;
}

.divider-text {
  background: white;
  padding: 0 16px;
  color: #999;
  font-size: 14px;
}

.social-login {
  display: flex;
  gap: 16px;
  margin-bottom: 32px;
}

.social-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.social-btn:hover {
  border-color: #ddd;
  background: #f8f8f8;
}

.social-btn.wechat:hover {
  border-color: #07C160;
  background: rgba(7, 193, 96, 0.05);
}

.social-btn.qq:hover {
  border-color: #12B7F5;
  background: rgba(18, 183, 245, 0.05);
}

.social-btn img {
  width: 24px;
  height: 24px;
}

.social-btn span {
  font-size: 14px;
  color: #333;
}

.register-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.link {
  color: #ff4757;
  cursor: pointer;
  margin-left: 4px;
}

.link:hover {
  text-decoration: underline;
}

.agreement {
  text-align: center;
  padding: 24px;
  font-size: 12px;
  color: #999;
  line-height: 1.5;
}

.agreement p {
  margin: 4px 0;
}

.agreement .link {
  color: #666;
  margin: 0 2px;
}
</style>
